package com.qumitech.visualnovel.service.lip.imp;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.lip.LipVo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.lip.LipService;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author :  ZhChNing
 * @Descriptiom :
 * @Date Created in 2018/12/28 15:41
 */
@Service
public class LipServiceImpl implements LipService {
    private final String LIP_CONFIG_URL = "/userprize/console/lip";
    private final String USER_STATE = "/userprize/console";

    @Override
    public Map<String, Object> getUserPridItem(Integer offset, Integer limit, Date startTime, Date endTime, Integer status) {
        String url = UrlConstant.SERVER_URL + LIP_CONFIG_URL;
        Map<String, String> paramMap = new HashMap<>();
        if (offset != null)
            paramMap.put("offset", offset.toString());
        if (limit != null)
            paramMap.put("limit", limit.toString());
        if (startTime != null)
            paramMap.put("startTime", startTime.toString());
        if (endTime != null)
            paramMap.put("endTime", endTime.toString());
        if (status != null)
            paramMap.put("status", status.toString());

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateLipState(LipVo lipVo) {
        String url = UrlConstant.SERVER_URL + USER_STATE;
        String req = JSONObject.toJSONString(lipVo);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }
}
